package com.virgo.demo.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.ListDataVo;
import com.virgo.common.exception.IApplicationException;
import com.virgo.demo.cms.converter.ArticleCateConverter;
import com.virgo.demo.cms.domain.ArticleCateDo;
import com.virgo.demo.cms.domain.ArticleDo;
import com.virgo.demo.cms.domain.bo.ArticleCateBo;
import com.virgo.demo.cms.domain.vo.ArticleCateVo;
import com.virgo.demo.cms.mapper.ArticleCateMapper;
import com.virgo.demo.cms.mapper.ArticleMapper;
import com.virgo.demo.cms.service.ArticleCateService;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ArticleCateServiceImpl extends ServiceImpl<ArticleCateMapper, ArticleCateDo> implements ArticleCateService {

	private final ArticleCateMapper articleCateMapper;

	private final ArticleMapper articleMapper;

	private final ArticleCateConverter articleCateConverter;

	/**
	 * 根据code查找
	 * @param code String
	 * @return ArticleCateDo
	 */
	@Override
	public ArticleCateDo loadByCode(String code) {
		LambdaQueryWrapper<ArticleCateDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(ArticleCateDo::getCode,code);
		queryWrapper.last(" limit 1 ");
		return getOne(queryWrapper);
	}

	/**
	 * 编辑文章分类
	 * @param articleCateBo ArticleCateBo
	 */
	@Override
	public void saveArticleCate(ArticleCateBo articleCateBo) {
		ArticleCateDo exist = loadByCode(articleCateBo.getCode());
		ArticleCateDo articleCateDo = new ArticleCateDo();
		BeanUtils.copyProperties(articleCateBo,articleCateDo);
		if (!ObjectUtils.isEmpty(exist) && !exist.getId().equals(articleCateDo.getId())) {
			throw new IApplicationException("文章分类-" + articleCateDo.getCode() + "已存在");
		}
		if (ObjectUtils.isEmpty(articleCateDo.getId()) || articleCateDo.getId().compareTo(0L) < 1) {
			save(articleCateDo);
			return;
		}
		updateById(articleCateDo);
	}

	/**
	 * 修改状态
	 * @param id     Long
	 * @param status Long
	 */
	@Override
	public void updateStatus(Long id, Integer status) {
		ArticleCateDo articleCateDo = new ArticleCateDo();
		articleCateDo.setId(id);
		articleCateDo.setStatus(status);
		updateById(articleCateDo);
	}

	/**
	 * 删除文章分类
	 * @param codes String
	 */
	@Override
	public void deleteArticleCate(String codes) {
		List<String> cateCodeList = Arrays.asList(codes.split(","));
		LambdaQueryWrapper<ArticleDo> articleDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
		articleDoLambdaQueryWrapper.in(ArticleDo::getCateCode,cateCodeList);
		long advNum = articleMapper.selectCount(articleDoLambdaQueryWrapper);
		if (advNum > 0) {
			throw new IApplicationException("存在文章，不能删除");
		}
        LambdaQueryWrapper<ArticleCateDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ArticleCateDo::getCode,cateCodeList);
		remove(queryWrapper);
	}

	/**
	 * 文章分类列表
	 * @param request HttpServletRequest
	 * @return ListDataVo<ArticleCateVo>
	 */
	@Override
	public ListDataVo<ArticleCateVo> findList(HttpServletRequest request) {
		long currentPage = IRequestUtils.getLong(request,"page");
		long pageSize = IRequestUtils.getLong(request,"pageSize");
		currentPage = Math.max(currentPage,1);
		pageSize = Math.max(pageSize,10);

		LambdaQueryWrapper<ArticleCateDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByDesc(ArticleCateDo::getId);

		String name = IRequestUtils.getString(request,"name","");
		if (!ObjectUtils.isEmpty(name)) {
			queryWrapper.like(ArticleCateDo::getName,name);
		}

		Page<ArticleCateDo> page = new Page<>(currentPage, pageSize);
		IPage<ArticleCateDo> iPage = articleCateMapper.selectPage(page, queryWrapper);
		return new ListDataVo<>(iPage.getTotal(),iPage.getPages(),articleCateConverter.convert(iPage.getRecords(), ArticleCateVo.class));
	}
}